package cn.hl.hm.controller;

import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverAmountInfo;
import cn.hl.hm.entity.Push;
import cn.hl.hm.entity.WithdrawApply;
import cn.hl.hm.service.DriverOwnInfoService;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.OrderService;
import cn.hl.hm.service.PushToAppIService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/withdrawController")
public class WithdrawController {
	private static final Logger logger = LoggerFactory.getLogger(WithdrawController.class);
	private final static SimpleDateFormat hhmmSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	DriverService driverService;
	@Autowired
	OrderService orderService;
	@Autowired
	private DriverOwnInfoService driverOwnInfoService;
	@Autowired
	private PushToAppIService pushToAppIService;
	/**
	 * 司机历史账单
	 * @param driverId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/historyInfo", method = RequestMethod.POST)
	public String historyInfo( @RequestParam Integer driverId,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("司机历史账单---withdrawController/historyInfo");
		request.setCharacterEncoding("UTF-8");
		try {
			DriverAmountInfo selectDriverHistory = driverService.selectDriverHistory(driverId);
			Result.ok(selectDriverHistory, InfoCode.SUCCESS, response);
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	/**
	 * 申请
	 * @param driverId
	 * @param driverName
	 * @param withdrawCash
	 * @param withdrawBankCardNum
	 * @param bankCardUserName
	 * @param withdrawInfo
	 * @param withdrawApply
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
//	@RequestMapping(value = "/apply", method = RequestMethod.POST)
	public String apply( @RequestParam Integer driverId,
			@RequestParam String driverName,
			@RequestParam BigDecimal withdrawCash,
			@RequestParam String withdrawBankCardNum,
			@RequestParam String bankCardUserName	,
			@RequestParam String withdrawInfo,
			@RequestParam String bankCardName,
			WithdrawApply withdrawApply,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("司机申请提现---withdrawController/apply");
		request.setCharacterEncoding("UTF-8");
		try {
			Driver driver = new Driver();
			driver.setId(driverId);
			List<Driver> selectDriver = driverService.selectDriver(driver);
			if(selectDriver!=null&&selectDriver.size()==1) {
				Driver d = selectDriver.get(0);
				String name = d.getName();
				if(StringUtils.isBlank(name)||(!name.equals(withdrawApply.getDriverName()))) {
					Result.ok(withdrawApply, InfoCode.PARAM_ERROR, response);
					return null;
				}
				
				DriverAmountInfo selectDriverHistory = driverService.selectDriverHistory(driverId);
				if(selectDriverHistory.getCanWithdrawCash().compareTo(withdrawCash)<0) {
					Result.ok(withdrawApply, InfoCode.PARAM_ERROR, response);
					return null;
				}else if (selectDriverHistory.getRechargeCash().compareTo(withdrawCash)<0) {
					Result.ok(withdrawApply, InfoCode.PARAM_ERROR, response);
					return null;
				}
				
				List<WithdrawApply> apply = driverService.getApply(driverId);
				if(apply!=null&&apply.size()>0) {
					Result.ok(withdrawApply, InfoCode.UNDO, response);
					return null;
				}
				int addApply = driverService.addApply(withdrawApply);
				if(addApply>0) {
					Result.ok(withdrawApply, InfoCode.SUCCESS, response);
					Push push = new Push();
					push.setBizzType(Push.BizzTypeEnum.USER.getName());
					push.setPtype(Push.PushTypeEnum.USERPUSH8.getId());
					push.setPushoid("");
					push.setDisplayType("notification");// message:// 消息，通知：notification
					push.setOperationType(1);// 用户类型：1司机，2发货端
					push.setPushClient("driverPushClient");// 使用的推送Client
					push.setPushType("listcast");// unicast：单播 // listcast：列表
					push.setTokenStr(new String[]{d.getDeviceToken()}); // 要发送手机的token
					// ios & android推送公共调用方法
					push.setContext("退款申请提交成功，我们会尽快审核并退款给您，请耐心等待。");// 要发送的内容
					push.setTitle("退款申请提交成功"); // 发送的标题
					driverOwnInfoService.insertSelective(push.getContext(),driverId);
					pushToAppIService.pushCommon(push);
					return null;
				}else {
					Result.ok(withdrawApply, InfoCode.FAILURE, response);
					return null;
				}
			}else {
				Result.ok(withdrawApply, InfoCode.PARAM_ERROR, response);
				return null;
			}
		
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	
	
	/**
	 * 审核
	 * @param id
	 * @param withdrawStatus 2 通过 3不通过
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/auditing", method = RequestMethod.POST)
	public String auditing( @RequestParam Integer id,
			@RequestParam String withdrawStatus,
			@RequestParam String operUser,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("审核提现申请---withdrawController/auditing");
		request.setCharacterEncoding("UTF-8");
		try {
			if(withdrawStatus.equals("2")||withdrawStatus.equals("3")) {
				
			}else {
				Result.ok(null, InfoCode.PARAM_ERROR, response);
				return null;
			}
			
			WithdrawApply withdrawApply  = new WithdrawApply();
			withdrawApply.setId(id);
			withdrawApply.setWithdrawStatus(withdrawStatus);
			withdrawApply.setOpenUser(operUser);
			int auditApply = driverService.auditApply(withdrawApply);
			if(auditApply>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
}
